import 'package:flutter/material.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x00_aleret_item.dart';
import 'package:flutter_demo/start_up/function_widgets/alerts/0x01_alert_dialog_routes.dart';

class SimpleDialogPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {


////////////////////////////////////////////////////////////////////////
    ///
    /// SimpleDialog也是Material组件库提供的对话框，它会展示一个列表，用于列表选择的场景。
    /// 
    void _showSimpleDialog() async {
      int i = await showDialog<int>(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: const Text("Select Language"),
            children: <Widget>[
              SimpleDialogOption(
                onPressed: () => Navigator.pop(context, 1),
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    vertical: 6
                  ),
                  child: const Text("Chinese"),
                ),
              ),
              SimpleDialogOption(
                onPressed: () => Navigator.pop(context, 2),
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    vertical: 6
                  ),
                  child: const Text("English"),
                ),
              )
            ],
          );
        }
      );

      if (i != null) {
        alert(context, content: "Your Choose : ${ i == 1 ? "Chinese" : "English"}");
      }
    }

    Future<int> _showSimpleDialog_x({String title, List<String> chooices = const ["Cancel"]}) {
      return showDialog<int>(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: title == null ? null : Text(title),
            children: chooices.map((value) {
              return SimpleDialogOption(
                onPressed: () => Navigator.pop(context, chooices.indexOf(value)),
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    vertical: 6
                  ),
                  child: Text(value),
                ),
              );
            }).toList(),
          );
        }
      );
    }

    void _showSimpleDialog_1() async {
      int i = await _showSimpleDialog_x(
        title: "Chooice Language",
        chooices: [
          "English",
          "Chinese",
          "Janpanese",
          "Spanish"
        ]
      );

      print("index $i");
    }


    /////////////////////////////////////////////////////////////////////////////


    return Scaffold(
      appBar: AppBar(
        title: Text("Simple Dialog"),
      ),
      body: Padding(
        padding: EdgeInsets.only(
          top: 18,
          bottom: 18
        ),
        child: ListView(
          children: <Widget>[
            AlertItem("text", _showSimpleDialog),
            AlertItem("Simple Dialog 1", _showSimpleDialog_1),
          ],
        ),
      ),
    );
  }
}